﻿using AutoMapper;
using Durian.API.Application.Commands.Process;
using IMES.Common.Domain.DurianModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Durian.API.Application.Commands.Assembly
{
    public class AssemblySelectHandler : IRequestHandler<AssemblySelectCommand, Result>
    {
        private readonly IMapper mapper;
        private readonly ILogger<ProcessManagementCreateHandler> logger;
        private readonly IBaseRepositorys<ProcessManagement> processManagementRepository;
        private readonly IBaseRepositorys<SystemLogs> systemLogsRepository;
        private readonly IBaseRepositorys<Assemblies> assembliesRepository;
        private readonly IBaseRepositorys<OrderProducts> orderProductsRepository;

        public AssemblySelectHandler(IMapper mapper, ILogger<ProcessManagementCreateHandler> logger, IBaseRepositorys<ProcessManagement> processManagementRepository, IBaseRepositorys<SystemLogs> systemLogsRepository, IBaseRepositorys<Assemblies> assembliesRepository, IBaseRepositorys<OrderProducts> orderProductsRepository)
        {
            this.mapper = mapper;
            this.logger = logger;
            this.processManagementRepository = processManagementRepository;
            this.systemLogsRepository = systemLogsRepository;
            this.assembliesRepository = assembliesRepository;
            this.orderProductsRepository = orderProductsRepository;
        }

        /// <summary>
        /// 装配工单显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(AssemblySelectCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var res = new Result() { Code = (int)CodeEnum.search };
                var process = assembliesRepository.GetAll().Where(x => x.Deletes == false);
                res.Code = (int)CodeEnum.search;
                res.Data = process;
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "装配工单管理显示和查询失败");
                return new Result() { Code = (int)CodeEnum.error, Messege = "装配工单管理显示和查询失败" };
            }
        }

        
    }
}
